# -*- coding: utf-8 -*-
# @description: 一个简单的描述符例子
# @File  : v1.py
# @Time  :2019-09-24 10:31:32
# @Author:selfeasy
# @Email :gm_selfeasy@163.com
class description:
    def __init__(self,name):
        self.name=name
    def __set__(self,instance,value):#self表示描述符实例，即item.weight，instance是托管实例，即item的实例,value表示对描述符赋值
        if value>0:
            instance.__dict__[self.name]=value
        else:
            raise ValueError(str(self.name)+' must >0')


class item:
    def __init__(self):
        self.name =123
    weight=description('weight')
    price=description('price')

if __name__ == "__main__":
    test=item()
    test.weight=21
    test.price=10
    print(test.__dict__)
    print (test.weight,test.price)  #21 10